package com.ibm.cps.processors;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ibm.cps.message.MessageFields;
import com.ibm.util.JSONUtility;
import com.ibm.util.exception.CPSException;

public class ProcessorsGroup {

	public static final String DEFAULT_GROUPTID = "000000000000000000000000";
	String groupid;

	public ProcessorsGroup(String groupType, String groupid) {
		this.groupid = groupid;
	}

	public ProcessorsGroup(String input) throws CPSException {
		ObjectNode node = JSONUtility.fromObjectString(input);
		JSONUtility.checkNotNull(node, MessageFields.GROUPID);
		groupid = node.get(MessageFields.GROUPID).asText();
	}

	public String getGroupid() {
		return groupid;
	}

	public static boolean isGroupType(String type) {
		return type.equals(MessageFields.GROUP_TYPE_TAB)
				|| type.equals(MessageFields.GROUP_TYPE_SQL);
	}

	public static String getGroupTypeFieldName(String type) {
		return MessageFields.GROUPID;
	}

	public static String generateDefaultGroup(String tenantid) {
		ObjectNode node = JSONUtility.newObjectNode();
		node.put(MessageFields.TYPE, MessageFields.GROUP_TYPE_TAB);
		node.put(MessageFields.GROUPID, tenantid);
		node.put(MessageFields.SOURCE, MessageFields.NODERED);
		node.put("label", "Node-red default");
		return node.toString();
	}
}
